/* 
 * File:   JServerWorker.h
 * Author: johnneyshi
 *
 * Created on 2013年1月27日, 下午8:07
 */

#ifndef JSERVERWORKER_H
#define	JSERVERWORKER_H
#include <string>
#include "JServer.h"
#include "JWorkerThread.h"
#include <queue>
#include "JLog.h"

struct WorkerArg
{
    std::string ip;
    int port;
    int weight;
    int threadCount;
    int threadMaxCount;
    int currentWorkCount;
    WorkerArg()
    {
        currentWorkCount = 0;
    }
    void Print()
    {
        JLog::Log(JLogMessage,"IP:%s Port:%d Weight:%d ThreadCount:%d MaxThreadCount:%d\n",ip.c_str(),port,weight,threadCount,threadMaxCount);
    }
};

//采用多线程
class JServerWorker : public JServer
{
public:
    JServerWorker();
    JServerWorker(const JServerWorker& orig);
    void Init(std::string _configFileName);
    void Init(std::string _ip,int _port,int _weight,int _threadCount,int _threadMaxCount);
    void Start();
    virtual ~JServerWorker();
    virtual void DealEpollFd(int fd);
    virtual void DealNewConnfd(int newFd);
    int GetWeight();
    friend void ThreadMain();
    friend class JWorkerThread;
    
    static pthread_mutex_t adapterConnFdMutex;
    static pthread_cond_t adapterConnFdCond;
    int GetCurrentWorkCount();
    //这个指针是后期加入，因为在JWorkerThreadSVN里面需要一个JServerStub指针，所以通过这个参数传递给JWorkerThreadSVN
    void* point1;
private:
    int currentWorkCount;
    
    WorkerArg workerArg;
    std::queue<int> needDealFds;
    virtual void MakeThreads(int _threadCount);
    void AddNewThread();
    int currentThreadCount;
    void ReadConfig(std::string _configFileName);
};

#endif	/* JSERVERWORKER_H */

